
//
//  SEGBaseDataModel.m
//  DBTest
//
//  Created by 刘山 on 2017/7/10.
//  Copyright © 2017年 Sam. All rights reserved.
//

#import "SEGBaseDataModel.h"
@implementation SEGBaseDataModel
- (void)encodeWithCoder:(NSCoder *)encoder
{
    unsigned int count = 0;
    objc_property_t *propertes = class_copyPropertyList([self class], &count);
    for (int i=0; i<count; i++) {
        const char *propertyName = property_getName(propertes[i]);
        NSString *name = [NSString stringWithUTF8String:propertyName];
        id value = [self valueForKey:name];
        [encoder encodeObject:value forKey:name];
    }
}

- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super init]) {
        unsigned int count =0;
        objc_property_t *propertes = class_copyPropertyList([self class], &count);
        for (int i = 0; i < count; i++) {
            const char *propertyName = property_getName(propertes[i]);
            NSString *name = [NSString stringWithUTF8String:propertyName];
            id value = [decoder decodeObjectForKey:name];
            [self setValue:value forKey:name];
        }
        
    }
    return self;
}

-(id)copyWithZone:(NSZone *)zone
{
    id copyModel = [[[self class] allocWithZone:zone] init];
    unsigned int count = 0;
    objc_property_t *propertes = class_copyPropertyList([copyModel class], &count);
    for (int i=0; i<count; i++) {
        const char *propertyName = property_getName(propertes[i]);
        NSString *name = [NSString stringWithUTF8String:propertyName];
        id value = [[copyModel valueForKey:name] copyWithZone:zone];
        [copyModel setValue:value forKey:name];
    }
    return copyModel;
}

-(id)mutableCopyWithZone:(NSZone *)zone
{
    id copyModel = [[[self class] allocWithZone:zone] init];
    unsigned int count = 0;
    objc_property_t *propertes = class_copyPropertyList([copyModel class], &count);
    for (int i=0; i<count; i++) {
        const char *propertyName = property_getName(propertes[i]);
        NSString *name = [NSString stringWithUTF8String:propertyName];
        id value = [[copyModel valueForKey:name] mutableCopyWithZone:zone];
        [copyModel setValue:value forKey:name];
    }
    return copyModel;
}


//model转字典
-(NSDictionary *)transform_InTo_Dictionary
{
    if(self){
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        unsigned int propsCount;
        
        objc_property_t *props = class_copyPropertyList([self class], &propsCount);
        
        for(int i = 0;i < propsCount; i++) {
            
            objc_property_t prop = props[i];
            NSString *propName = [NSString stringWithUTF8String:property_getName(prop)];
            id value = [self valueForKey:propName];
            if(value == nil) {
                value = @"";
            } else {
                if ([value respondsToSelector:@selector(isKindOfClass:)]) {//有可能是Bool或常量等等
                    value = [self getObjectInternal:value];
                }
            }
            [dic setObject:value forKey:propName];
        }
        
        return dic;

    }
    return @{};
}

-(id)getObjectInternal:(id)obj {
    if([obj isKindOfClass:[NSString class]]
       ||
       [obj isKindOfClass:[NSNumber class]]
       ||
       [obj isKindOfClass:[NSNull class]]
       ) {
        return obj;
    }
    if([obj isKindOfClass:[NSArray class]]) {
        
        NSArray *objarr = obj;
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:objarr.count];
        
        for(int i = 0; i < objarr.count; i++) {
            
            [arr setObject:[self getObjectInternal:[objarr objectAtIndex:i]] atIndexedSubscript:i];
        }
        return arr;
    }
    if([obj isKindOfClass:[NSDictionary class]]) {
        
        NSDictionary *objdic = obj;
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:[objdic count]];
        
        for(NSString *key in objdic.allKeys) {
            
            [dic setObject:[self getObjectInternal:[objdic objectForKey:key]] forKey:key];
        }
        return dic;
    }
    return [obj transform_InTo_Dictionary];
}



-(instancetype)segAction_SEGEveryClass:(id)params
{
    id Class = [self class];
    if ( ! params) {
        return [[[Class alloc] init] initWithDictionary:@{}];
    }
    return [[[Class alloc] init] initWithDictionary:params];
}

-(void)subModelSetParams:(id)params
{
    //runtime解耦操作时使用，在子类中实现此方法。用于接收初始化时需要的参数，配合SEGUhomebkMediator+Standard使用。
}

-(void)notFound:(id)params
{
}
@end
